package hczc.longcai.com.hccrowdfunding.utils.httputils;




import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 从网络上下载数据
 * Created by Administrator on 2016/2/23.
 */
public class LoadDatas {


    private static final String TAG = "LoadDatas";

    public static byte[] getDatas(String path) {
        byte[] temp = null;
        String c_path = path;
        HttpURLConnection connection = null;
        InputStream in = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            URL url = new URL(c_path);
            connection = ((HttpURLConnection) url.openConnection());
            connection.setConnectTimeout(5 * 1000);
            if (connection.getResponseCode() == 200) {
                in = connection.getInputStream();
                byte[] buffer = new byte[1024];
                int i;
                while ((i = in.read(buffer)) != -1) {
                    out.write(buffer, 0, i);
                }
            }
            temp = out.toByteArray();
            return temp;

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return temp;
    }

}
